<?php

namespace app\admin\controller;
use app\admin\controller\Base;

/**
 * 卡密种类控制器
 */
class Cardtype extends Base
{


    //列表场景
    public function cardtypelist()
    {
        # code...
        $cardtypes=model('Cardtype')->with('software')->paginate(10);
        $viewData=[
            'cardtypes'=>$cardtypes

        ];
        
        $this->assign($viewData);
        return view();
    }

    //添加场景
    public function cardtypeadd()
    {
        # code...
        if (request()->isAjax()) {

            do
            {
                $cardtype_key = bin2hex(random_bytes(4));
            }
            while (db('cardtype')->where('cardtype_key', $cardtype_key)->find());

            
            $data=[
                'cardtypename'=>input('post.cardtypename'),
                'cardtypesoftware'=>input('post.cardtypesoftware'),
                'cardtypetype'=>input('post.cardtypetype'),
                'cardtypetime'=>input('post.cardtypetime'),
                'cardtype_key'=>$cardtype_key
            ];
           
            $result=model('Cardtype')->add($data);
            if ($result==1) {
                $this->success('卡密类型添加成功','admin/cardtype/cardtypelist');
            }else{
                $this->error($result);
            }

        }
        $softwares=model('Software')->select();
        $viewData=[
            'softwares'=>$softwares
        ];
        $this->assign($viewData);
        return view();

    }
    

    //编辑场景
    public function cardtypeedit()
    {
        # code...
        if (request()->isAjax()) {
             
            $data=[
                'id'=>input('post.id'),
                'cardtypename'=>input('post.cardtypename'),
                'cardtypesoftware'=>input('post.cardtypesoftware'),
                'cardtypetype'=>input('post.cardtypetype'),
                'cardtypetime'=>input('post.cardtypetime')
            ];

            $result=model('Cardtype')->edit($data);
            if ($result==1) {
                $this->success('卡密类型修改成功','admin/cardtype/cardtypelist');
            }else{
                $this->error($result);
            }
            
        }
        $cardtypeInfo=model('Cardtype')->find(input('id'));        
        $softwares=model('Software')->select();
        
        $viewData=[
            'cardtypeInfo'=>$cardtypeInfo,
            'softwares'=>$softwares

        ];
        
        $this->assign($viewData);
        return view();
    }


    //删除场景
    public function cardtypedel()
    {
        # code...
        $cardtypeInfo=model('Cardtype')->find(input('post.id'));
		$result=$cardtypeInfo->delete();
		if($result){
			$this->success('栏目删除成功','admin/cardtype/cardtypelist');
		}
		else{
			$this->error($result);
		}
    }

}